﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.IO;

namespace WebGetter
{
    public class PageRender
    {
        private Page page;

        private const string PAGE_TEMPLATE_NAME = "templates\\page.tem";
        private const string STYLE_TEMPLATE_NAME = "templates\\style.tem";
        private const string REPLY_TEMPLATE_NAME = "templates\\reply.tem";
        private const string REPLY_SPLITTER_TEMPLATE_NAME = "templates\\replysplitter.tem";

        private static readonly string defaultPageTemplate;
        private static readonly string defaultReplyTemplate;
        private static readonly string defaultReplySplitter;

        public static string PageTemplte { get; set; }
        public static string Style { get; set; }
        public static string ReplyTemplate { get; set; }
        public static string ReplySplitter { get; set; }

        static PageRender()
        {
            defaultReplyTemplate = "<p>{0}</p>";
            defaultReplySplitter = "</br>";
            defaultPageTemplate = "<html>" +
                                "    <head>" +
                                "        <style type=\"text/css\">" +
                                "            {0}" +
                                "        </style>" +
                                "    </head>" +
                                "    <body>" +
                                "        {1}" +
                                "    </body>" +
                                "</html>";

            ReloadTemplate();
        }

        /// <summary>
        /// init all template here
        /// </summary>
        public static void ReloadTemplate()
        {
            Style = ReadContent(STYLE_TEMPLATE_NAME, string.Empty);
            PageTemplte = ReadContent(PAGE_TEMPLATE_NAME, defaultPageTemplate);
            ReplyTemplate = ReadContent(REPLY_TEMPLATE_NAME, defaultReplyTemplate);
            ReplySplitter = ReadContent(REPLY_SPLITTER_TEMPLATE_NAME, defaultReplySplitter);
        }

        private static void InitDefaultTemplate()
        {

        }

        private static string ReadContent(string path, string defaultValue)
        {
            var content = File.ReadAllText(path);
            if (string.IsNullOrEmpty(content))
                content = defaultValue;

            return content;
        }

        public PageRender(Page page)
        {
            this.page = page;
        }

        public string GetContent()
        {
            return GetContent(null);
        }

        public string GetContent(Func<Reply, bool> func)
        {
            var replies = func != null ? page.Replies.Where(func) : page.Replies;

            string content = string.Format(PageTemplte, Style, BuildReplies(replies));
            return content;
        }

        private string BuildReplies(IEnumerable<Reply> replies)
        {
            return string.Join(ReplySplitter, replies.Select(r => BuildReply(r)).ToArray());
        }

        private static string BuildReply(Reply r)
        {
            return ReplyTemplate.Replace("{Floor}", r.Floor)
                  .Replace("{Author}", r.Author)
                  .Replace("{PostTime}", r.PostTime.ToShortDateString())
                  .Replace("{Content}", r.Content);
        }
    }
}
